<?php
	require_once( "functions/inc_systemFunctions.php" );
	page_header( "Wayne Schlegel Inc. | Profile", true, "myflight" );
?>

<?php
	$currentUser = getUserData( $_SESSION['user_id'] );


	if( empty( $_POST['send'] ) ){
		$_POST['gender']               = $currentUser['gender'];
		$_POST['firstName']            = $currentUser['firstName'];
		$_POST['lastName']             = $currentUser['lastName'];
		$_POST['email']                = $currentUser['email'];
		$_POST['url']                  = $currentUser['url'];
		$_POST['street']               = $currentUser['street'];
		$_POST['postCode']             = $currentUser['postCode'];
		$_POST['city']                 = $currentUser['city'];
		$_POST['primaryPhoneNumber']   = $currentUser['primaryPhoneNumber'];
		$_POST['secondaryPhoneNumber'] = $currentUser['secondaryPhoneNumber'];
		$_POST['mobilePhoneNumber']    = $currentUser['mobilePhoneNumber'];
		$_POST['faxNumber']            = $currentUser['faxNumber'];
		$_POST['countryCode']          = $currentUser['countryCode'];
		$_POST['languageCode']         = $currentUser['languageCode'];
		$_POST['username']             = $currentUser['username'];
		$_POST['password']             = $currentUser['password'];
	}
?>

<?php
	$errorCount      = 0;

	if( !empty( $_POST['send'] ) && $_POST['send'] == "register" ) {
		// check for mandatory fields
		if( empty( $_POST['gender'] ) )       { $errorCount++; }
		if( empty( $_POST['firstName'] ) )    { $errorCount++; }
		if( empty( $_POST['lastName'] ) )     { $errorCount++; }
		if( empty( $_POST['email'] ) )        { $errorCount++; }
		if( empty( $_POST['countryCode'] ) )  { $errorCount++; }
		if( empty( $_POST['languageCode'] ) ) { $errorCount++; }
		if( empty( $_POST['username'] ) )     { $errorCount++; }
		if( empty( $_POST['password'] ) )     { $errorCount++; }
		
		if( $errorCount == 0 ) {
			// save new user
			$now       = date( "Y-m-d H:i:s" );
			$unique_id = uniqueId();
			
			$sql   = "UPDATE contactData SET gender='" . $_POST['gender'] . "', firstName='" . $_POST['firstName'] . "', lastName='" . $_POST['lastName'] . "', email='" . $_POST['email'] . "', url='" . $_POST['url'] . "', street='" . $_POST['street'] . "', postCode='" . $_POST['postCode'] . "', city='" . $_POST['city'] . "', primaryPhoneNumber='" . $_POST['primaryPhoneNumber'] . "', secondaryPhoneNumber='" . $_POST['secondaryPhoneNumber'] . "', mobilePhoneNumber='" . $_POST['mobilePhoneNumber'] . "', faxNumber='" . $_POST['faxNumber'] . "', countryCode='" . $_POST['countryCode'] . "', languageCode='" . $_POST['languageCode'] . "', lastWriteAccess='" . $now . "' WHERE id = " . $currentUser['contactData_id'];
			$res = mysql_query( $sql );
			if( !$res ) {
				echo "ERROR while updating contactData | " . mysql_error();
			}
			$contactData_id = mysql_insert_id();
			
			$sql2  = "UPDATE userData SET username='" . $_POST['username'] . "', password='" . $_POST['password'] . "', lastWriteAccess='" . $now . "' WHERE id = " . $_SESSION['user_id'];
			$res2  = mysql_query( $sql2 );
			if( !$res2 ) {
				echo "ERROR while updating userData | " . mysql_error();
			}
			if( $res && $res2 ){
				echo "<p class=\"green\">Your profile is updated successfully!</p>";
			}
		}
	}
	
#	if( empty( $_POST['send'] ) || $_POST['send'] != "register" || $errorCount > 0 ) {
?>
	<h1>Profile</h1>
	<?php
		if( $errorCount >= 1 ){
			echo "<p class=\"error\">Please fill at least all mandatory fields (marked with an astersik).</p>";
		}
	?>
	<form method="post" action="" class="iForm formTable">
		<select name="gender" style="clear: both;">
			<option value=""      >&mdash; select your gender * &mdash;</option>
			<option value="MALE"  <?php if( $_POST['gender'] == "MALE" )   echo "selected=\"selected\" "; ?>>male</option>
			<option value="FEMALE"<?php if( $_POST['gender'] == "FEMALE" ) echo "selected=\"selected\" "; ?>>female</option>
		</select>
		<label class="placeholder" for="firstName">first name *</label>
		<input type="text" name="firstName"            value="<?=$_POST['firstName']?>"            autocorrect="off" title=""  />
		<label class="placeholder" for="lastName">last name *</label>
		<input type="text" name="lastName"             value="<?=$_POST['lastName']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="email">e-mail *</label>
		<input type="text" name="email"                value="<?=$_POST['email']?>"                autocorrect="off" title=""  />
		<label class="placeholder" for="url">homepage</label>
		<input type="text" name="url"                  value="<?=$_POST['url']?>"                  autocorrect="off" title=""  />
		<label class="placeholder" for="street">street</label>
		<input type="text" name="street"               value="<?=$_POST['street']?>"               autocorrect="off" title=""  />
		<label class="placeholder" for="postCode">post code</label>
		<input type="text" name="postCode"             value="<?=$_POST['postCode']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="city">city</label>
		<input type="text" name="city"                 value="<?=$_POST['city']?>"                 autocorrect="off" title=""  />
		phone numbers<br />
		<label class="placeholder" for="primaryPhoneNumber">primary</label>
		<input type="text" name="primaryPhoneNumber"   value="<?=$_POST['primaryPhoneNumber']?>"   autocorrect="off" title=""  />
		<label class="placeholder" for="secondaryPhoneNumber">secondary</label>
		<input type="text" name="secondaryPhoneNumber" value="<?=$_POST['secondaryPhoneNumber']?>" autocorrect="off" title=""  />
		<label class="placeholder" for="mobilePhoneNumber">mobile</label>
		<input type="text" name="mobilePhoneNumber"    value="<?=$_POST['mobilePhoneNumber']?>"    autocorrect="off" title=""  />
		<label class="placeholder" for="faxNumber">fax number</label>
		<input type="text" name="faxNumber"            value="<?=$_POST['faxNumber']?>"            autocorrect="off" title=""  />
		<select name="countryCode" title="">
			<option value=""      >&mdash; select your country * &mdash;</option>
			<option value="BE" <?php if( $_POST['countryCode'] == "BE" ) echo "selected=\"selected\""; ?>>Belgium</option>
			<option value="DE" <?php if( $_POST['countryCode'] == "DE" ) echo "selected=\"selected\""; ?>>Germany</option>
			<option value="NL" <?php if( $_POST['countryCode'] == "NL" ) echo "selected=\"selected\""; ?>>The Netherlands</option>
		</select>
		<select name="languageCode" title="">
			<option value="">&mdash; select your preferred language * &mdash;</option>
			<option value="EN" <?php if( $_POST['languageCode'] == "EN" ) echo "selected=\"selected\""; ?>>english</option>
		</select><br />
		<br />

		<label class="placeholder" for="username">username *</label>
		<input type="text" name="username"             value="<?=$_POST['username']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="password">password *</label>
		<input type="password" name="password"             value="<?=$_POST['password']?>"             autocorrect="off" title=""  />

		<label for="send">&nbsp;</label><input type="submit" name="send" value="save profile" />

	</form>
<?php 
#	} 
?>


<?php
	page_footer();
?>